package bb.lanxing.activity.sns

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.lifecycle.ViewModelProvider
import bb.lanxing.R
import bb.lanxing.manager.DeepLinkManager.Companion.DeepLinkData
import bb.lanxing.databinding.ActivitySnsactivityBinding
import bb.lanxing.mvvm.base.BaseDBActivity


class SNSActivity : BaseDBActivity<ActivitySnsactivityBinding>() {

    private val viewModel: SnsViewModel by lazy {
        ViewModelProvider(this)[SnsViewModel::class.java]
    }

    override fun registerMessageEvent(): Boolean {
        return true
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_snsactivity
    }


    override fun initView(binding: ActivitySnsactivityBinding) {
        setupActionBar(true)
        var serializableExtra =
            intent.getSerializableExtra(SNSActivityKt.PARAM_SNS_PLATFORM) as SNSPlatform?
        if (serializableExtra == null) {
            serializableExtra = SNSPlatform.STRAVA
        }

        if (serializableExtra == SNSPlatform.STRAVA) {
            setupActionBar(true)
            title = getString(R.string.st_strava)
            //            switchFragment(SnsPlatformFragment.Companion.newInstance(SNSPlatform.STRAVA));
        }
    }

    /*public final void switchFragment(SnsPlatformFragment fragment) {
        ActivitySnsactivityBinding activitySnsactivityBinding = (ActivitySnsactivityBinding) this.binding;
        if (activitySnsactivityBinding != null) {
            FragmentTransaction beginTransaction = getSupportFragmentManager().beginTransaction();
//            Intrinsics.checkNotNullExpressionValue(beginTransaction, "supportFragmentManager.beginTransaction()");
            beginTransaction.replace(activitySnsactivityBinding.container.getId(), fragment);
            beginTransaction.commitAllowingStateLoss();
        }
    }*/
    override fun onMessage(i: Int, i2: Int, i3: Int, obj: Any, bundle: Bundle): Boolean {
        var queryParameter: String?
        if (i == R.id.msg_app_deep_link_response) {
            if (obj !is DeepLinkData) {
                return true
            }
            dismissLoadingDialog()
            val rawUri = obj.rawUri
            if (rawUri == null || (rawUri.getQueryParameter("code" /*a.i*/)
                    .also { queryParameter = it }) == null
            ) toast(R.string.parse_data_error)
            return true
        }
        return super.onMessage(i, i2, i3, obj, bundle)
    }
    companion object{
        fun start(context: Context, snsPlatform: SNSPlatform) {
            val intent = Intent(context, SNSActivity::class.java)
            intent.putExtra(SNSActivityKt.PARAM_SNS_PLATFORM, snsPlatform)
            context.startActivity(intent)
        }
    }
}
